package com.skycong.hello77.complexforms.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.skycong.hello77.R;
import com.skycong.hello77.complexforms.adapter.AbstractPanelListAdapter;
import com.skycong.hello77.complexforms.entity.DataComparisonLsit;
import com.skycong.hello77.complexforms.entity.DataComparisonVO;
import com.skycong.hello77.complexforms.view.PanelListLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * 复杂表格
 * @author zyb
 */
public class FormSecondActivity extends Activity {

    private PanelListLayout pl_root;
    private ListView lv_content;
    public int width;
    private AbstractPanelListAdapter adapter;

    private List<List<String>> contentList = new ArrayList<>();

    private List<Integer> itemWidthList = new ArrayList<>();
    private List<String> leftTitle = new ArrayList<>();
    private List<String> topTitle = new ArrayList<>();

    private List<String> rowDataList = new ArrayList<>();
    public List<DataComparisonVO> items = new ArrayList<>();
    private TextView top_title;//顶部固定标题



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form_activity_item);
        top_title = (TextView) findViewById(R.id.tv_title);
        getWidth();
        initView();

        initRowDataList();
        initItemWidthList();

        initAdapter();
        pl_root.setAdapter(adapter);
        reload();

        // 注意：
        // 如果你决定自己实现自己的Column，而不是使用默认的1，2，3。。。
        // 请注意更新contentList时手动更新columnList
    }

    public void getWidth() {
        WindowManager manager = this.getWindowManager();
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);
        int widthPixels = dm.widthPixels;         // 屏幕宽度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        width = (int) (widthPixels / density) / 3;  // 屏幕宽度的3分之1(dp)
    }

    private void reload() {

        initDatas(DataComparisonLsit.getListData());
    }

    private void initAdapter(){
        adapter = new AbstractPanelListAdapter(this, pl_root, lv_content,items,top_title) {
            @Override
            protected BaseAdapter getContentAdapter() {
                return null;
            }
        };
        adapter.setInitPosition(0);
        adapter.setSwipeRefreshEnabled(true);
        adapter.setRowDataList(rowDataList);// must have
        adapter.setTitle("example");// optional
        adapter.setTitleHeight(40);
        adapter.setTitleWidth(width);
        adapter.setOnRefreshListener(new CustomRefreshListener());// optional
        adapter.setContentDataList(contentList);// must have
        adapter.setItemWidthList(itemWidthList);// must have
        adapter.setColumnDataList(leftTitle);
        adapter.setItemHeight(40);// optional, dp
    }

    private void initDatas(List<DataComparisonLsit> datas) {
        items.clear();
        for (int i = 0; i < datas.size(); i++) {
            items.add(new DataComparisonVO(datas.get(i).title,
                    "",
                    "",
                    "",
                    false,
                    false,
                    -1));
            if (datas.get(i).items != null && datas.get(i).items.size() > 0) {
                for (int j = 0; j < datas.get(i).items.size(); j++) {
                    items.add(new DataComparisonVO(datas.get(i).items.get(j).name,
                           datas.get(i).items.get(j).value+"",
                           datas.get(i).items.get(j).compareValue+"",
                           datas.get(i).items.get(j).increase+"",
                            datas.get(i).items.get(j).endNode,
                            datas.get(i).items.get(j).percent,
                            datas.get(i).items.get(j).id));
                }
            }
        }
         Log.i("msg","data数据结构"+items);
        initContentDataList();
        adapter.notifyDataSetChanged();
        adapter.getColumnAdapter().notifyDataSetChanged();

//
//        initAdapter();
//        viewModel.pl_root.setAdapter(adapter);
    }




    private void initView() {

        pl_root = (PanelListLayout) findViewById(R.id.id_pl_root);
        lv_content = (ListView) findViewById(R.id.id_lv_content);


        //设置listView为多选模式，长按自动触发
//        lv_content.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
//        lv_content.setMultiChoiceModeListener(new MultiChoiceModeCallback());
        /*
        * 设置listView为多选模式，长安自动触发
        * lv_content.setChoiceMode(ListView.CENTICE_MODE_MULTIPLE_MODAL);
        * iv_content.setMultiChoiceModeListener(new MultiChoiceModeCallback);
        * iv_content.setOnScrollListener(this);
        * iv.content.setOnitemClik
        * */

        //listView的点击监听
        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(FormSecondActivity.this, "你选中的position为：" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }


    public class CustomRefreshListener implements SwipeRefreshLayout.OnRefreshListener {
        @Override
        public void onRefresh() {
            // you can do sth here, for example: make a toast:
            Toast.makeText(FormSecondActivity.this, "custom SwipeRefresh listener", Toast.LENGTH_SHORT).show();
            // don`t forget to call this
            adapter.getSwipeRefreshLayout().setRefreshing(false);
        }
    }

    /**
     * 生成一份横向表头的内容
     *
     * @return List<String>
     */
    private void initRowDataList() {
        rowDataList.add("第一列");
        rowDataList.add("第二列");
        rowDataList.add("第三列");
        rowDataList.add("第四列");
        rowDataList.add("第五列");
        rowDataList.add("第六列");
        rowDataList.add("第七列");
    }

    /**
     * 初始化content数据
     */
    private void initContentDataList() {
        if (items != null && items.size() > 0) {
            contentList.clear();
            for (int i = 0; i < items.size(); i++) {
                List<String> data = new ArrayList<>();
                data.add(items.get(i).value);
                data.add(items.get(i).compareValue);
                data.add(items.get(i).increase);
                data.add(items.get(i).increase);
                data.add(items.get(i).increase);
                data.add(items.get(i).increase);
                data.add(items.get(i).increase);
                contentList.add(data);
            }
        }
            Log.i("msg","内外list循环结构"+contentList);

    }

    /**
     * 初始化 content 部分每一个 item 的每个数据的宽度
     */
    private void initItemWidthList() {
        itemWidthList.clear();
        itemWidthList.add(width);
        itemWidthList.add(width);
        itemWidthList.add(width);
        itemWidthList.add(width);
        itemWidthList.add(width);
        itemWidthList.add(width);
        itemWidthList.add(width);
    }

    /**
     * 更新content数据源
     */
    private void changeContentDataList() {
        contentList.clear();
        for (int i = 1; i < 10; i++) {
            List<String> data = new ArrayList<>();
            data.add("第" + i + "第一个");
            data.add("第" + i + "第二个");
            data.add("第" + i + "第三个");
            data.add("第" + i + "第四个");
            data.add("第" + i + "第五个");
            data.add("第" + i + "第六个");
            data.add("第" + i + "第七个");
            contentList.add(data);
        }
    }

    /**
     * 插入一个数据
     */
    private void insertData() {
        List<String> data = new ArrayList<>();
        data.add("插入1");
        data.add("插入2");
        data.add("插入3");
        data.add("插入4");
        data.add("插入5");
        data.add("插入6");
        data.add("插入7");
        contentList.add(5, data);
    }

    /**
     * 删除一个数据
     */
    private void removeData() {
        contentList.remove(10);
    }

//    /**
//     * 多选模式的监听器
//     */
//    private class MultiChoiceModeCallback implements ListView.MultiChoiceModeListener {
//
//        private View actionBarView;
//        private TextView tv_selectedCount;
//
//        /**
//         * 进入ActionMode时调用
//         * 可设置一些菜单
//         *
//         * @param mode
//         * @param menu
//         * @return
//         */
//
//
//        /**
//         * 和onCreateActionMode差不多的时机调用，不写逻辑
//         *
//         * @param mode
//         * @param menu
//         * @return
//         */
//        @Override
//        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
//            return false;
//        }

        /**
         * 当ActionMode的菜单项被点击时
         *
         * @param mode
         * @param item
         * @return
         */
//        @Override
//        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//            switch (item.getItemId()) {
//                StringBuilder checkedItemString = new StringBuilder();
//                for (int i = 0; i < checkedItemPositionList.size(); i++) {
//                    checkedItemString.append(checkedItemPositionList.get(i) + ",");
//                }
//
//                    Toast.makeText(MainActivity.this, "你选中的position有：" + checkedItemString, Toast.LENGTH_SHORT).show();
//                    break;
//                default:
//                    break;
//            }
//            return true;
//        }

        /**
         * 退出ActionMode时调用
         *
         * @param mode
         */
//        @Override
//        public void onDestroyActionMode(ActionMode mode) {
//            lv_content.clearChoices();
//        }

        /**
         * 当item的选中状态发生改变时调用
         *
         * @param mode
         * @param position
         * @param id
         * @param checked
         */
//        @Override
//        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
//            int selectedCount = lv_content.getCheckedItemCount();
//            tv_selectedCount.setText(String.valueOf(selectedCount));
//            ((ArrayAdapter) lv_content.getAdapter()).notifyDataSetChanged();
//        }
    //}
}
